//
//  HomeViewController.m
//  Mtime
//
//  Created by wang xinkai on 15/7/28.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeModel.h"
#import "HomeCell.h"
#import "CitiesViewController.h"
#import "BgView.h"//背面视图

#import "HomeHeaderView.h"//头部轮播图

@interface HomeViewController ()
{
    UITableView *_homeTableView;//电影列表
    
    BgView *_bgView;//背面的视图
    
    NSMutableArray *_dataList;//model数组（使单元格与model一一对应）
    
    
    //navigationBar的左侧按钮
    UIButton *leftBtn;
}
@end

@implementation HomeViewController

-(id)init{

    if (self = [super init]) {
        
        self.title = @"首页";
        self.tabBarItem.image = [UIImage imageNamed:@"home"];
        
        [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_top_movie_background_cover"]]];
    }
    return self;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatNavigatinBarItem];//创建导航栏按钮
    
    [self loadData];//加载数据
    
    [self creatTableView];
    
}

- (void)LeftItemAction:(UIButton *)sender {

    CitiesViewController *citiesVC = [[CitiesViewController alloc] init];
    citiesVC.title = leftBtn.titleLabel.text;
    //6.调用block的方法
    [citiesVC addBlock:^(NSString *cityName) {
       
        [leftBtn setTitle:cityName forState:UIControlStateNormal];
    }];
    citiesVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:citiesVC animated:YES];

}

- (void)creatNavigatinBarItem {

    leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    leftBtn.frame = CGRectMake(0, 0, 100, 25);
    [leftBtn setTitle:@"北京" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(LeftItemAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 0, 49, 25);
    btn1.tag = 1001;
    [btn1 addTarget:self action:@selector(rightItemAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(0, 0, 49, 25);
    btn2.tag = 1002;
    [btn2 addTarget:self action:@selector(rightItemAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [btn2 setHidden:YES];
    
    UIView *custemView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 49, 25)];
    [custemView addSubview:btn1];
    [custemView addSubview:btn2];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:custemView];
}

- (void)rightItemAction:(UIButton *)sender {

    UIView *customView = self.navigationItem.rightBarButtonItem.customView;
    
    sender.hidden = YES;
    
    if (sender.tag == 1001) {
        
        UIButton *btn = (UIButton *)[customView viewWithTag:1002];
        btn.hidden = NO;
        
        _bgView.hidden = NO;
        
    }else {
    
        UIButton *btn = (UIButton *)[customView viewWithTag:1001];
    
        btn.hidden = NO;
        
        _bgView.hidden = YES;
    }
    
    [self flipWithView:self.view isLeft:sender.tag == 1001?YES:NO];
    [self flipWithView:customView isLeft:sender.tag == 1001?YES:NO];
}

- (void)flipWithView:(UIView *)supView isLeft:(BOOL)left {

    //视图动画的过渡形式
    [UIView animateWithDuration:.35 animations:^{
       
        /*
         setAnimationTransition:(过渡形式)transition forView:(做翻转的视图)view cache:(yes)cache;
         */
        [UIView setAnimationTransition:left? UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight forView:supView cache:YES];
    }];
}


- (void)loadData {

    //！！！！初始化model数组
    _dataList = [[NSMutableArray alloc] init];
    
    /*
     *一：解析json数据
     */
    //1.获得json文件的路径
    NSString *pathFile = [[NSBundle mainBundle] pathForResource:@"home_header" ofType:@"json"];
    
    //2.将json文件转换成nsdata形式
    NSData *data = [NSData dataWithContentsOfFile:pathFile];
    
    //3.解析json文件《利用与json文件的存储形式相同的形式来接受》
    NSDictionary *homeDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
    /*
     *二：将解析的数据存入model
     */
    NSArray *movies = [homeDic objectForKey:@"movies"];
    
    for (NSDictionary *dic in movies) {
        
        HomeModel *model = [[HomeModel alloc] init];
        //给model属性赋值
        model.titleCn = [dic objectForKey:@"titleCn"];
        model.titleEn = [dic objectForKey:@"titleEn"];
        model.img = [dic objectForKey:@"img"];
        model.ratingFinal = [dic objectForKey:@"ratingFinal"];
        model.commonSpecial = [dic objectForKey:@"commonSpecial"];
        model.wantedCount = [dic objectForKey:@"wantedCount"];
        [_dataList addObject:model];
    }

    [_homeTableView reloadData];
}

- (void)creatTableView {

    _homeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    _homeTableView.backgroundColor = [UIColor clearColor];
    
    _homeTableView.delegate = self;
    _homeTableView.dataSource = self;
    
    [self.view addSubview:_homeTableView];

    
    _bgView = [[BgView alloc] initWithFrame:_homeTableView.frame];
    
    _bgView.dataList = _dataList;
    
    _bgView.hidden = YES;
    
    [self.view addSubview:_bgView];
    
    
    HomeHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"HomeHeaderView" owner:nil options:nil] lastObject];
    
    headerView.dataArray = _dataList;
    
    _homeTableView.tableHeaderView = headerView;
    
    
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"homeCell"];
    
    if (!cell) {
        
//        cell = [[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"homeCell"];
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:nil options:nil] lastObject];
        
    }
    /*
     *三：使model与cell一一对应
     */
     cell.model = _dataList[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 110;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
